%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%     TABLE 7             %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Option demand - PANEL A
% FM regressions - Panel A
clear betas b t_stat
x = find(ismember(floor(dates_m/100), floor(date_ise/100))); 
% Align with data from ISE which is from 01/2006 to 04/2016

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Change Y_Var for different specifications
Y_Var = Call_buying_demand; % Net buying demand from ISE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Var1 = (Stock_PRisk(x,:)); 
Var2 = Stock_PSentiment(x,:);
Var3 = Stock_Betas_EPU(x,:);
Var4 = log(Stock_CAP(x,:));
Var5 = Stock_BM(x,:);
Var6 = Stock_IdioVol(x,:);
Var7 = Stock_reversal(x,:);
Var8 = Stock_Momentum(x,:);
Var9 = Stock_illiquidity(x,:);
Var10 = Stock_Inst(x,:);
Var11 = Stock_Leverage(x,:);
Var12 = Stock_GrossProfit(x,:);

NW = 4; % Newey-West lags

format short
for i = 1:size(Var1,1)
    betas(i,:) = regress(Y_Var(i,:)',[ones(size(Y_Var(:,:),2),1), Var1(i,:)'  ...
            Var2(i,:)' Var3(i,:)' Var4(i,:)'  Var5(i,:)' ...
            Var6(i,:)' Var7(i,:)' Var8(i,:)' Var9(i,:)' Var10(i,:)' ...
            Var11(i,:)' Var12(i,:)' ...
            ]);                                           
end
clear stats 

for i = 1:size(betas,2)
   nw_stats = nwest(betas(:,i),ones(size(betas(:,i))),NW);
   b(i,1) = nw_stats.beta;
   t_stat(i,1) = nw_stats.tstat;
end

clear nw_stats Betas1 CAP1 i returns Var1 Var2 beta NW ...
    Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 x X


%% Option demand - PANEL B
clear  b1 t_stat1 b2 t_stat2 betas
x = find(ismember(floor(dates_m/100), floor(date_ise/100))); 
% Align with data from ISE which is from 01/2006 to 04/2016

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Change Y_Var for different specifications
Y_Var = call_deltahedge_returns_rb(x,:)*100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Var1 = (Stock_PRisk(x,:)); 
Var2 = Stock_PSentiment(x,:);
Var3 = Stock_Betas_EPU(x,:);
Var4 = log(Stock_CAP(x,:));
Var5 = Stock_BM(x,:);
Var6 = Stock_IdioVol(x,:);
Var7 = Stock_reversal(x,:);
Var8 = Stock_Momentum(x,:);
Var9 = Stock_illiquidity(x,:);
Var10 = Stock_Inst(x,:);
Var11 = Stock_Leverage(x,:);
Var12 = Stock_GrossProfit(x,:);

NW = 4; % Newey-West lags

% When Demand is high 
Y_Var1 = Y_Var;
Y_Var1(find(High_Demand_Dummy==0))=nan;
format short
for i = 1:size(Var1,1)
    betas(i,:) = regress(Y_Var1(i,:)',[ones(size(Y_Var1(:,:),2),1), Var1(i,:)'  ...
            Var2(i,:)' Var3(i,:)' Var4(i,:)'  Var5(i,:)' ...
            Var6(i,:)' Var7(i,:)' Var8(i,:)' Var9(i,:)' Var10(i,:)' ...
            Var11(i,:)' Var12(i,:)']);     warning off                                      
end
clear stats 

for i = 1:size(betas,2)
   nw_stats = nwest(betas(:,i),ones(size(betas(:,i))),NW);
   b1(i,1) = nw_stats.beta;
   t_stat1(i,1) = nw_stats.tstat;
end

betas1 = betas(:,2);


% When Demand is low
Y_Var2 = Y_Var;
Y_Var2(find(High_Demand_Dummy==1))=nan;
format short
for i = 1:size(Var1,1)
    betas(i,:) = regress(Y_Var2(i,:)',[ones(size(Y_Var2(:,:),2),1), Var1(i,:)'  ...
            Var2(i,:)' Var3(i,:)' Var4(i,:)'  Var5(i,:)' ...
            Var6(i,:)' Var7(i,:)' Var8(i,:)' Var9(i,:)' Var10(i,:)' ...
            Var11(i,:)' Var12(i,:)']);         warning off                                  
end
clear stats 

for i = 1:size(betas,2)
   nw_stats = nwest(betas(:,i),ones(size(betas(:,i))),NW);
   b2(i,1) = nw_stats.beta;
   t_stat2(i,1) = nw_stats.tstat;
end

betas2 = betas(:,2);

clear nw_stats t_stat i Var1 Var2 betas NW x ...
    Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Y_Var Y_Var1 Y_Var2

nw_stats = nwest(betas1-betas2,ones(size(betas1)),4);
ans = [nw_stats.beta(1); nw_stats.tstat(1)] % Difference between high and low demand and t-stat